﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class UserControls_FillShopInfo_New : UCBase
{
    public UserControls_FillShopInfo_New()
    {
        Name = "收货信息_2";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        rblPayType.Attributes.Add("onclick", "return selectPayType()");                 //选择支付方式
        rblCarriageType.Attributes.Add("onclick", "return selectCarriageType()");       //选择配送方式

        ddlProvince.Attributes.Add("onchange", "ddlProvinceChange(this)");
        ddlCity.Attributes.Add("onchange", "ddlCityChange(this)");
  
        if (!IsPostBack)
        {           
            hCartType.Value = Shove._Web.Utility.GetRequest("CartType");    //哪个购物车(会员推荐专区、百企网)

            BindData();

            int ProviceID = 0;      //省份ID
            DataTable dt = new DAL.Views.V_City().Open("", "[ID]=" + _Customer.CityID, "");

            if (dt != null && dt.Rows.Count > 0)
            {
                ProviceID = Shove._Convert.StrToInt(dt.Rows[0]["ProvinceID"].ToString(), 0);
                lbProvince.Text = dt.Rows[0]["Province"].ToString();
                lbCity.Text = dt.Rows[0]["City"].ToString();                
            }
            LoadProvince(ProviceID);
            LoadCity(ProviceID);
        }

        LoadPayType();          //加载支付方式
        LoadCarriageType();     //加载配送方式
        LoadShoppingInfo();     //加载购物车中的商品   
    }

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;
        Shove._Web.Session.SetSession(uConst.RequestLoginPage, Request.Url.AbsoluteUri);
        
        base.OnLoad(e);
    }

    private void BindData()
    {
        if (_Customer != null)
        {
            lbName.Text = _Customer.CustomerName;
            lbEmail.Text = _Customer.Email;
            lbTel.Text = _Customer.Tel;
            lbMobile.Text = _Customer.Mobile;
            lbPostCode.Text = _Customer.PostCode;
            lbAddress.Text = _Customer.Address;

            tbName.Text = _Customer.CustomerName;
            tbEmail.Text = _Customer.Email;
            tbTel.Text = _Customer.Tel;
            tbMobile.Text = _Customer.Mobile;
            tbPostCode.Text = _Customer.PostCode;
            tbAddress.Text = _Customer.Address;
        }

        //lbName.Text = "刘杰";
        //lbEmail.Text = "LLorJJ999@163.com";  
        //lbTel.Text = "0755-8856856"; 
        //lbMobile.Text = "15899798876"; 
        //lbPostCode.Text = "415000";   
        //lbAddress.Text = "深圳宝安区F518时尚创意园F16栋首层"; 
    }

    private void LoadPayType()
    {
        DataTable dt = null;

        dt = new DAL.Tables.tbl_Site_PayType().Open("","","");

        if (dt != null && dt.Rows.Count > 0)
        {
            rblPayType.DataSource = dt;
            rblPayType.DataTextField = "PayTypeName";
            rblPayType.DataValueField = "ID";
            rblPayType.DataBind();

            rblPayType.SelectedValue = dt.Rows[0]["ID"].ToString();
        }
    }

    private void LoadCarriageType()
    {
        DataTable dt = null;

        dt = new DAL.Tables.tbl_Site_CarriageType().Open("","[IsUse]=1","");

        if (dt != null && dt.Rows.Count > 0)
        {
            lbCarriageMoney.Text = dt.Rows[0]["Money"].ToString();
            lbCarriageMoney2.Text = dt.Rows[0]["Money"].ToString();

            rblCarriageType.DataSource = dt;
            rblCarriageType.DataTextField = "TypeName";
            rblCarriageType.DataValueField = "ID";
            rblCarriageType.DataBind();

            rblCarriageType.SelectedValue = dt.Rows[0]["ID"].ToString();
        }

    }

    /// <summary>
    /// 加载所购物品
    /// </summary>
    private void LoadShoppingInfo()
    {
        string Sum = "0";

        if (hCartType.Value == "carList2")
        {
            gvGood.DataSource = GoodOfCart.GetItems("carList2", ref Sum);
            gvGood.DataBind();
        }
        else
        {
            gvGood.DataSource = GoodOfCart.GetItems("carList", ref Sum);
            gvGood.DataBind();
        }

        lbGoodMoney.Text = Sum.ToString();
        lbSumMoney.Text = (Shove._Convert.StrToDouble(Sum, 0) + Shove._Convert.StrToDouble(lbCarriageMoney.Text, 0)).ToString();
    }

    protected void gvGood_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Style.Add("background-image", "url(images/UserControls/bg_hui_23_2.jpg)");
        }
    }

    //提交订单
    protected void ibNext_Click(object sender, EventArgs e)
    {
        long SalesID = 0;                                                                   //销售主表 ID                
        short SalesType = 4;                                                                //商城订单 Type = 4  商城会员推荐专区订单 Type = 5
        if (hCartType.Value == "carList2")
        {
            SalesType = 5;
        }

        for (int i = 0; i < rblCarriageType.Items.Count; i++)
        {
            if (rblCarriageType.Items[i].Selected)
            {
                string a = rblCarriageType.Items[i].Value;
            }
        }

        int PayType = Shove._Convert.StrToInt(rblPayType.SelectedValue, 0); ;               //支付方式 1 在线支付 2 网银支付 3 银行转帐 4 公司转帐
        int CarriageType = Shove._Convert.StrToInt(rblCarriageType.SelectedValue, 0);       //货运方式 1 快递(收取一定费用) 2 平邮(免费)
        bool IsTicket = false;                                                              //是否需要发票
        if (rblTicket.SelectedValue == "1")
        {
            IsTicket = true;
        }
 
        DAL.Procedures.P_Site_ShopAddSales_Master(_Customer.ID, _Site.ID, _Customer.ID, SalesType, tbRemark.Text, PayType, CarriageType, tbName.Text, tbMobile.Text, tbTel.Text, tbEmail.Text, tbPostCode.Text, tbAddress.Text, IsTicket, _Site.ID, ref SalesID);
        AddSalesDetail(SalesID);

        Response.Redirect("Default.aspx?PN=Cart3&SaleID=" + SalesID.ToString());
    }

    /// <summary>
    /// 增加商品到销售明细表
    /// </summary>
    /// <param name="id"></param>
    private void AddSalesDetail(long SalesID)
    {
        string Sum = "";
        ArrayList list = null;

        if (hCartType.Value == "carList2")
        {
            list = GoodOfCart.GetItems("carList2", ref Sum);
        }
        else
        {
            list = GoodOfCart.GetItems("carList", ref Sum);
        }

        for (int i = 0; i < list.Count; i++)
        {
            GoodOfCart Good = (GoodOfCart)list[i];

            double Money = Good.SumMoney + Shove._Convert.StrToDouble(lbCarriageMoney.Text, Good.SumMoney);

            DAL.Procedures.P_Shop_AddSales_Detail(SalesID, Good.ID, Good.Amount, Money, Good.GoodNorm, Good.GoodColor);
        }

        string fileName = "";
        if (hCartType.Value == "carList2")
        {
            fileName = "carList2";
        }
        else
        {
            fileName = "carList";
        }
        try
        {
            HttpCookie c = HttpContext.Current.Request.Cookies[fileName];
            c.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(c);
        }
        catch { }
    }

    /// <summary>
    /// 绑定省份
    /// </summary>
    private void LoadProvince(int ProvinceID)
    {
        ddlProvince.DataSource = BLL.CommBLL.GetProvince();
        ddlProvince.DataTextField = "Name";
        ddlProvince.DataValueField = "ID";
        ddlProvince.DataBind();

        ddlProvince.SelectedValue = ProvinceID.ToString();
    }

    private void LoadCity(int ProvinceID)
    {
        ddlCity.DataSource = Ajax.ShopCustomer.GetCity(ProvinceID);
        ddlCity.DataTextField = "Name";
        ddlCity.DataValueField = "ID";
        ddlCity.DataBind();

        ddlCity.SelectedValue = _Customer.CityID.ToString();
    }

}
